<template>
  <div class="add-form">
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="40%" @close="closeHandle()">
      <el-form ref="dataForm" :model="formBase" label-position="left" label-width="150px">
        <el-form-item label="分类编码：" prop="code">
          <el-input v-model="formBase.code" style="width: 200px;" placeholder="请输入分类编码"></el-input>
        </el-form-item>
        <el-form-item label="分类名称：" prop="name">
          <el-input v-model="formBase.name" style="width: 200px;"  placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input v-model="formBase.sort" type="text" style="width: 200px;" @input="validHandle" placeholder="请输入排序序号"></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <div class="imgInfo" v-if="formBase.image" @click="delete_img('image')">
            <img :src="formBase.image" width="150px" />
            <i class="el-icon-delete" style="display: none"></i>
          </div>
          <div v-else>
            <el-upload
              :action="$uploadUrl"
              :data="fullUpType"
              :class="{disabled: formBase.image}"
              :file-list="filelist"
              :on-success="uploadSuccess"
              :on-remove="handleRemove"
              list-type="picture-card"
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">取消</el-button>
        <el-button type="primary" @click="createData">确认</el-button>
      </div>

    </el-dialog>
  </div>
</template>
<script>
import { updateData, addData } from '@/api/componentType'
export default {
  name: 'add',
  props: ['dialogTitle'],
  data() {
    return {
      fullUpType: { type: 7, extension: 'img' },
      dialogFormVisible: false,
      formBase: {
        name: '',
        code: '',
        sort: '',
        class: '1'
      },
      filelist: []
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    dialogFormV(val) {
      this.dialogFormVisible = true
      if (val !== undefined) {
        this.formBase = val
      }
    },
    closeHandle() {
      this.formBase = {}
      this.filelist = []
    },
    validHandle(val) {
      const reg = /^[1-9]\d*|0$/
      if (!reg.test(val)) {
        this.formBase.sort = 0
      } else if (val > 9999) {
        this.$message.error('序号输入不能太大！')
        this.formBase.sort = 0
      }
    },
    // 弹层隐藏
    dialogFormH() {
      this.dialogFormVisible = false
    },
    delete_img(item) {
      this.formBase[item] = ''
    },
    handleRemove(file, fileList) {
      this.uploadDisabled = false
    },
    uploadSuccess(obj, file) {
      this.formBase.image = obj
    },
    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          this.dialogFormH()
          const parentId = this.$route.query.id
          let data = {
            ...this.formBase,
            parentId
          }
          console.log(data)
          if (this.formBase.id) {
            await updateData(data).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            await addData(data).then((res) => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    }
  },
  // 挂载结束

  mounted: function() {},
  // 创建完毕状态
  created() {
  },
  // 组件更新
  updated: function() {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
